<script setup>
import { ref, defineEmits } from 'vue'
import { artAddChannelService, artEditChannelService } from '@/api/article/article'
const emit = defineEmits(['success'])
const dialogVisible = ref(false)
const open = (row) => {
  dialogVisible.value = true
  form.value.resetFields()
  formModel.value = { ...row }
}
defineExpose({
  open
})
//表单
const formModel = ref({
  cate_name: '',
  cate_alias: ''
})
const rules = {
  cate_name: [
    { required: true, message: '请输入分类名称', trigger: 'blur' },
    {
      pattern: /^\S{1,10}$/,
      message: '分类名必须是1-10位的非空字符',
      trigger: 'blur'
    }
  ],
  cate_alias: [
    { required: true, message: '请输入分类别名', trigger: 'blur' },
    {
      pattern: /^[a-zA-Z0-9]{1,15}$/,
      message: '分类别名必须是1-15位的字母数字',
      trigger: 'blur'
    }
  ]
}
const form = ref()
const okSubmit = async () => {
  await form.value.validate()
  if (formModel.value.id) {
    const { data: res } = await artEditChannelService(formModel.value)
    ElMessage.success(res.message)
  } else {
    const { data: res } = await artAddChannelService(formModel.value)
    ElMessage.success(res.message)
  }
  dialogVisible.value = false
  emit('success')
}
</script>
<template>
  <el-dialog v-model="dialogVisible" :title="formModel.id ? '编辑分类' : '添加分类'" width="30%">
    <div>
      <el-form :model="formModel" :rules="rules" ref="form">
        <el-form-item prop="cate_name" label="分类名称">
          <el-input v-model="formModel.cate_name" placeholder="分类名称"
        /></el-form-item>
        <el-form-item prop="cate_alias" label="分类别名">
          <el-input v-model="formModel.cate_alias" placeholder="分类别名" />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="okSubmit"> 确认 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped></style>
